package com.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.community.dao.IBaseDao;
import com.community.entity.Article;
import com.community.service.IArticleService;
@Service
public class ArticleServiceImpl implements IArticleService{
	
	@Autowired
	private IBaseDao<Article> adao;

	@Override
	public List<Article> getArticleToClickNumByLimit(int limit) {
		// TODO Auto-generated method stub
		String sql = "select * from shengya_article where type=1 order by clickNum desc limit "+limit;
		
		List<Object[]> object = adao.findBySql(sql);
		List<Article> alist = new ArrayList<>();
		Article a ;
		for (Object[] objects : object) {
			a = new Article();
			a.setId(Long.valueOf(objects[0].toString()));
			a.setImagePath(objects[9].toString());
			alist.add(a);
		}
		
		return alist;
	}
	
	@Override
	public List<Article> getOtherArticleToClickNumByLimit(int limit) {
		// TODO Auto-generated method stub
		String sql = "select * from shengya_article where type=2 order by createTime desc limit "+limit;
		
		List<Object[]> object = adao.findBySql(sql);
		List<Article> alist = new ArrayList<>();
		Article a ;
		for (Object[] objects : object) {
			a = new Article();
			a.setId(Long.valueOf(objects[0].toString()));
			a.setTittle(objects[11].toString());
			a.setContent(objects[8].toString());
			a.setImagePath(objects[9].toString());
			alist.add(a);
		}
		
		return alist;
	}

	@Override
	public Article getArticleById(Long id) {
		// TODO Auto-generated method stub
		return adao.get(Article.class, id);
	}

	@Override
	public void updateArticle(Article a) {
		// TODO Auto-generated method stub
		adao.update(a);
	}

	@Override
	public List<Article> getArticleByLimit(int limit) {
		// TODO Auto-generated method stub
		String sql = "select * from shengya_article where type=1 order by createTime desc limit "+limit;
		
		List<Object[]> object = adao.findBySql(sql);
		List<Article> alist = new ArrayList<>();
		Article a ;
		for (Object[] objects : object) {
			a = new Article();
			a.setId(Long.valueOf(objects[0].toString()));
			a.setTittle(objects[11].toString());
			a.setContent(objects[8].toString());
			a.setImagePath(objects[9].toString());
			alist.add(a);
		}
		
		return alist;
	}

}
